package com.xinchuang.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.collection.ListUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xinchuang.common.KeyWordSystem;
import com.xinchuang.entity.target.TargetBaseData;
import com.xinchuang.entity.targetSystem.FontTable;
import com.xinchuang.entity.targetSystem.RangeType;
import com.xinchuang.entity.targetSystem.system.*;
import com.xinchuang.entity.targetSystem.target.*;
import com.xinchuang.mapper.targetSystem.*;
import com.xinchuang.mapper.targetSystem.target.SystemsRangeTypeMapper;
import com.xinchuang.pojoUtil.TargetUtil;
import com.xinchuang.service.RedisUseService;
import com.xinchuang.service.target.TargetBaseDataService;
import com.xinchuang.service.targetSystem.target.*;
import com.xinchuang.util.*;
import com.xinchuang.utils.ObjectUtil;
import com.xinchuang.utils.RedisName;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.tomcat.jni.File;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Flux;

import java.util.*;

/**
 * TODO
 *
 * @author zkwczx
 * @version 1.0
 * @date 2024/4/11 8:23
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class RedisUseServiceImpl implements RedisUseService {

    private final TargetTypeMapper targetTypeMapper;

    private final PostureTypeService postureTypeService;

    private final RedisUtil redisUtil;

    private final TargetBaseDataService targetBaseDataService;

    private final TargetMsgTableService targetMsgTableService;

    private final TargetSubRelationTableService targetSubRelationTableService;

    private final TargetSubMsgTableService targetSubMsgTableService;

    private final SystemPositionMapper systemPositionMapper;

    private final KeyWordSystem keyWordSystem;

    private final TargetPostureRelationTableService targetPostureRelationTableService;

    private final TargetPostureMsgTableService targetPostureMsgTableService;

    private final SystemsMasterMapper systemsMasterMapper;

    private final SystemsRelationMapper systemsRelationMapper;

    private final SystemsRangeTypeMapper systemsRangeTypeMapper;

    private final RangeTypeMapper rangeTypeMapper;

    private final SystemsSceneMapper systemsSceneMapper;

    @Override
    public void initTarget() {
        List<TargetBaseData> list = targetBaseDataService.list();
        if (CollectionUtil.isNotEmpty(list)) {
            Set<Object> set = new HashSet<>();
            for (TargetBaseData data : list) {
                set.add(data.getTargetId());
            }
            redisUtil.del(RedisName.IS_TARGET);
            redisUtil.sSet(RedisName.IS_TARGET, set.toArray());
        }
    }

    @Override
    public boolean isTarget(String id) {
        return redisUtil.sHasKey(RedisName.IS_TARGET, id);
    }

    @Override
    public void updateIsTarget(String id) {
        redisUtil.sSet(RedisName.IS_TARGET, id);
    }

    @Override
    public void initSub() {
        List<TargetSubRelationTable> relationTables = targetSubRelationTableService.list();
        if (CollectionUtil.isNotEmpty(relationTables)) {
            Set<Object> set = new HashSet<>();
            for (TargetSubRelationTable table : relationTables) {
                set.add(table.getSubId());
            }
            redisUtil.del(RedisName.IS_SUB);
            redisUtil.sSet(RedisName.IS_SUB, set.toArray());
        }
    }

    @Override
    public boolean isSub(String id) {
        return redisUtil.sHasKey(RedisName.IS_SUB, id);
    }

    @Override
    public void updateIsSub(String id) {
        redisUtil.sSet(RedisName.IS_SUB, id);
    }

    @Override
    public void initTypeSign() {
        Map<String, Object> map = new HashMap<>();
        List<Map<String, String>> signAll = targetTypeMapper.getTypeSignAll();
        if (CollectionUtil.isNotEmpty(signAll)) {
            for (Map<String, String> iconMap : signAll) {
                map.put(iconMap.get("id"), iconMap.get("icon"));
            }
        }
        List<Map<String, String>> postureTypeSignAll = targetTypeMapper.getPostureTypeSignAll();
        if (CollectionUtil.isNotEmpty(postureTypeSignAll)) {
            for (Map<String, String> iconMap : postureTypeSignAll) {
                map.put(iconMap.get("id"), iconMap.get("icon"));
            }
        }
        redisUtil.del(RedisName.SIGN_TARGET_ICON_ALL);
        redisUtil.hmset(RedisName.SIGN_TARGET_ICON_ALL, map);
    }

    @Override
    public void initTypeName() {
        {
            Map<String, Object> map = new HashMap<>();
            List<Map<String, String>> typeNameAll = targetTypeMapper.getTypeNameAll();
            if (CollectionUtil.isNotEmpty(typeNameAll)) {
                for (Map<String, String> typeMap : typeNameAll) {
                    map.put(typeMap.get("id"), typeMap.get("surname"));
                }
            }
            redisUtil.del(RedisName.SIGN_TARGET_NAME_ALL);
            redisUtil.hmset(RedisName.SIGN_TARGET_NAME_ALL, map);
        }
        {
            Map<String, Object> map = new HashMap<>();
            List<Map<String, String>> typeSubNameAll = targetTypeMapper.getTypeSubNameAll();
            if (CollectionUtil.isNotEmpty(typeSubNameAll)) {
                for (Map<String, String> typeMap : typeSubNameAll) {
                    map.put(typeMap.get("id"), typeMap.get("surname"));
                }
            }
            redisUtil.del(RedisName.SIGN_SUB_NAME_ALL);
            redisUtil.hmset(RedisName.SIGN_SUB_NAME_ALL, map);
        }
        {
            Map<String, Object> map = new HashMap<>();
            List<PostureType> postureTypes = postureTypeService.list();
            if (CollectionUtil.isNotEmpty(postureTypes)) {
                for (PostureType typeMap : postureTypes) {
                    map.put(typeMap.getTypeId(), typeMap.getTypeName());
                }
            }
            redisUtil.del(RedisName.SIGN_POSTURE_NAME_ALL);
            redisUtil.hmset(RedisName.SIGN_POSTURE_NAME_ALL, map);
        }
    }

    @Override
    public void updateTypeSign(String typeId, String sign) {
        redisUtil.hset(RedisName.SIGN_TARGET_ICON_ALL, typeId, sign);
    }

    @Override
    public String getTypeSign(String typeId) {
        if (StringUtils.isBlank(typeId)) {
            return "default.png";
        }
        Object o = redisUtil.hget(RedisName.SIGN_TARGET_ICON_ALL, typeId);
        return ObjectUtil.isEmpty(o) ? "default.png" : o.toString();
    }

    @Override
    public void updateTypeName(String typeId, String typeName) {
        redisUtil.hset(RedisName.SIGN_TARGET_NAME_ALL, typeId, typeName);
    }

    @Override
    public String getTypeName(String typeId) {
        if (StringUtils.isBlank(typeId)) {
            return "未知类型";
        }
        Object o = redisUtil.hget(RedisName.SIGN_TARGET_NAME_ALL, typeId);
        return ObjectUtil.isEmpty(o) ? "未知类型" : o.toString();
    }

    @Override
    public void updateSubTypeName(String typeId, String typeName) {
        redisUtil.hset(RedisName.SIGN_SUB_NAME_ALL, typeId, typeName);
    }

    @Override
    public String getSubTypeName(String typeId) {
        if (StringUtils.isBlank(typeId)) {
            return "未知类型";
        }
        Object o = redisUtil.hget(RedisName.SIGN_SUB_NAME_ALL, typeId);
        return ObjectUtil.isEmpty(o) ? "未知类型" : o.toString();
    }

    @Override
    public void updatePostureTypeName(String typeId, String typeName) {
        redisUtil.hset(RedisName.SIGN_POSTURE_NAME_ALL, typeId, typeName);
    }

    @Override
    public String getPostureTypeName(String typeId) {
        if (StringUtils.isBlank(typeId)) {
            return "未知类型";
        }
        Object o = redisUtil.hget(RedisName.SIGN_POSTURE_NAME_ALL, typeId);
        return ObjectUtil.isEmpty(o) ? "未知类型" : o.toString();
    }

    @Override
    public void initTargetBase() {
        List<TargetBaseData> targetBaseDataList = targetBaseDataService
                .lambdaQuery()
                .eq(TargetBaseData::getDeleteSign, 1)
                .list();
        for (TargetBaseData targetBaseData : targetBaseDataList) {
            if (redisUtil.hasKey(RedisName.TARGET_BASE_DATA_ALL + "_" + targetBaseData.getTargetId())) {
                continue;
            }
            List<TargetMsgTable> msgTables = targetMsgTableService
                    .lambdaQuery()
                    .eq(TargetMsgTable::getRedId, targetBaseData.getTargetId())
                    .list();
            Map<String, Object> baseMap = new HashMap<>();
            Map<String, Object> specialMap = new HashMap<>();
            for (TargetMsgTable msgTable : msgTables) {
                if (1 == msgTable.getMsgType()) {
                    baseMap.put(msgTable.getKeyId(), FileElseUtil.getObjectToString(msgTable.getKeyValue()));
                } else if (2 == msgTable.getMsgType()) {
                    specialMap.put(msgTable.getKeyId(), FileElseUtil.getObjectToString(msgTable.getKeyValue()));
                }
            }
            redisUtil.del(RedisName.TARGET_BASE_DATA_ALL + "_" + targetBaseData.getTargetId());
            redisUtil.hmset(RedisName.TARGET_BASE_DATA_ALL + "_" + targetBaseData.getTargetId(), baseMap);
            redisUtil.del(RedisName.TARGET_SPECIAL_DATA_ALL + "_" + targetBaseData.getTargetId());
            redisUtil.hmset(RedisName.TARGET_SPECIAL_DATA_ALL + "_" + targetBaseData.getTargetId(), specialMap);
        }
    }

    @Override
    public void updateTargetBase(String targetId, List<TargetMsgTable> msgTables) {
        for (TargetMsgTable msgTable : msgTables) {
            if (1 == msgTable.getMsgType()) {
                redisUtil.hset(RedisName.TARGET_BASE_DATA_ALL + "_" + targetId, msgTable.getKeyId(), FileElseUtil.getObjectToString(msgTable.getKeyValue()));
            }
        }
    }

    @Override
    public void updateTargetSpecial(String targetId, List<TargetMsgTable> msgTables) {
        redisUtil.del(RedisName.TARGET_SPECIAL_DATA_ALL + "_" + targetId);
        for (TargetMsgTable msgTable : msgTables) {
            if (2 == msgTable.getMsgType()) {
                redisUtil.hset(RedisName.TARGET_SPECIAL_DATA_ALL + "_" + targetId, msgTable.getKeyId(), FileElseUtil.getObjectToString(msgTable.getKeyValue()));
            }
        }
    }

    @Override
    public void initTargetSubRelation() {
        List<TargetBaseData> targetBaseData = targetBaseDataService.list();
        if (CollectionUtil.isNotEmpty(targetBaseData)) {
            for (TargetBaseData target : targetBaseData) {
                if (redisUtil.hasKey(RedisName.TARGET_SUB_RELATION_LIST + "_" + target.getTargetId())) {
                    continue;
                }
                List<TargetSubRelationTable> relationTables = targetSubRelationTableService.lambdaQuery().eq(TargetSubRelationTable::getTargetId, target.getTargetId()).list();
                if (CollectionUtil.isNotEmpty(relationTables)) {
                    List<String> ids = new ArrayList<>();
                    for (TargetSubRelationTable relationTable : relationTables) {
                        ids.add(relationTable.getSubId());
                    }
                    redisUtil.sSet(RedisName.TARGET_SUB_RELATION_LIST + "_" + target.getTargetId(), ids.toArray());
                }
            }
        }
    }

    @Override
    public void updateTargetSubRelation(boolean isAdd, String targetId, String subId) {
        if (isAdd) {
            redisUtil.sSet(RedisName.TARGET_SUB_RELATION_LIST + "_" + targetId, subId);
        } else {
            redisUtil.setRemove(RedisName.TARGET_SUB_RELATION_LIST + "_" + targetId, subId);
        }
    }

    @Override
    public String getTargetTypeIdOrName(String targetId, boolean isName) {
        Object classify = redisUtil.hget(RedisName.TARGET_BASE_DATA_ALL + "_" + targetId, "targetClassify");
        if (ObjectUtil.isNotEmpty(classify)) {
            List list = FileElseUtil.getObjectToList(classify);
            if (isName) {
                return getTypeName(list.get(list.size() - 1).toString());
            } else {
                return list.get(list.size() - 1).toString();
            }
        }
        return null;
    }

    @Override
    public void initSubBase() {
        List<TargetSubRelationTable> subRelationTables = targetSubRelationTableService.list();
        if (CollectionUtil.isNotEmpty(subRelationTables)) {
            for (TargetSubRelationTable subRelationTable : subRelationTables) {
                if (redisUtil.hasKey(RedisName.TARGET_BASE_DATA_ALL + "_" + subRelationTable.getSubId())) {
                    continue;
                }
                List<TargetSubMsgTable> subMsgTables = targetSubMsgTableService
                        .lambdaQuery()
                        .eq(TargetSubMsgTable::getSubId, subRelationTable.getSubId())
                        .list();
                Map<String, Object> baseMap = new HashMap<>();
                Map<String, Object> specialMap = new HashMap<>();
                for (TargetSubMsgTable msgTable : subMsgTables) {
                    if (1 == msgTable.getMsgType()) {
                        baseMap.put(msgTable.getKeyId(), FileElseUtil.getObjectToString(msgTable.getKeyValue()));
                    } else if (2 == msgTable.getMsgType()) {
                        specialMap.put(msgTable.getKeyId(), FileElseUtil.getObjectToString(msgTable.getKeyValue()));
                    }
                }
                redisUtil.del(RedisName.TARGET_BASE_DATA_ALL + "_" + subRelationTable.getSubId());
                redisUtil.hmset(RedisName.TARGET_BASE_DATA_ALL + "_" + subRelationTable.getSubId(), baseMap);
                redisUtil.del(RedisName.TARGET_SPECIAL_DATA_ALL + "_" + subRelationTable.getSubId());
                redisUtil.hmset(RedisName.TARGET_SPECIAL_DATA_ALL + "_" + subRelationTable.getSubId(), specialMap);
            }
        }
    }

    @Override
    public void updateSubBase(String subId, List<TargetSubMsgTable> msgTables) {
        for (TargetSubMsgTable msgTable : msgTables) {
            if (1 == msgTable.getMsgType()) {
                redisUtil.hset(RedisName.TARGET_BASE_DATA_ALL + "_" + subId, msgTable.getKeyId(), FileElseUtil.getObjectToString(msgTable.getKeyValue()));
            }
        }
    }

    @Override
    public void updateSubSpecial(String subId, List<TargetSubMsgTable> msgTables) {
        redisUtil.del(RedisName.TARGET_SPECIAL_DATA_ALL + "_" + subId);
        for (TargetSubMsgTable msgTable : msgTables) {
            if (2 == msgTable.getMsgType()) {
                redisUtil.hset(RedisName.TARGET_SPECIAL_DATA_ALL + "_" + subId, msgTable.getKeyId(), FileElseUtil.getObjectToString(msgTable.getKeyValue()));
            }
        }
    }

    @Override
    public void initPostureData() {
        List<TargetPostureRelationTable> list = targetPostureRelationTableService.list();
        for (TargetPostureRelationTable targetPostureRelationTable : list) {
            if (redisUtil.hasKey(RedisName.TARGET_BASE_DATA_ALL + "_" + targetPostureRelationTable.getPostureId())) {
                continue;
            }
            List<TargetPostureMsgTable> postureMsgTables = targetPostureMsgTableService.lambdaQuery().eq(TargetPostureMsgTable::getPostureId, targetPostureRelationTable.getPostureId()).list();
            Map<String, Object> map = new HashMap<>();
            for (TargetPostureMsgTable msgTable : postureMsgTables) {
                map.put(msgTable.getKeyId(), FileElseUtil.getObjectToString(msgTable.getKeyValue()));
            }
            redisUtil.del(RedisName.TARGET_BASE_DATA_ALL + "_" + targetPostureRelationTable.getPostureId());
            redisUtil.hmset(RedisName.TARGET_BASE_DATA_ALL + "_" + targetPostureRelationTable.getPostureId(), map);
        }
    }

    @Override
    public void updatePostureBase(String postureId, List<TargetPostureMsgTable> msgTables) {
        for (TargetPostureMsgTable msgTable : msgTables) {
            if (1 == msgTable.getMsgType()) {
                redisUtil.hset(RedisName.TARGET_BASE_DATA_ALL + "_" + postureId, msgTable.getKeyId(), FileElseUtil.getObjectToString(msgTable.getKeyValue()));
            } else if (2 == msgTable.getMsgType()) {
                redisUtil.hset(RedisName.TARGET_SPECIAL_DATA_ALL + "_" + postureId, msgTable.getKeyId(), FileElseUtil.getObjectToString(msgTable.getKeyValue()));
            }
        }
    }

    @Override
    public void initPostureRelation() {
        List<TargetPostureRelationTable> list = targetPostureRelationTableService.list();
        if (CollectionUtil.isNotEmpty(list)) {
            for (TargetPostureRelationTable table : list) {
                redisUtil.sSet(RedisName.POSTURE_RELATION_LIST + "_" + table.getTargetId(), table.getPostureId());
            }
        }
    }

    @Override
    public void updatePostureRelation(String redId, String postureId) {
        redisUtil.sSet(RedisName.POSTURE_RELATION_LIST + "_" + redId, postureId);
    }

    @Override
    public void initView() {
        List<SystemPosition> positionList = systemPositionMapper.selectList(null);
        if (CollectionUtil.isNotEmpty(positionList)) {
            for (SystemPosition position : positionList) {
                if (redisUtil.hasKey(RedisName.VIEW_POSITION_DATA_ALL + "_" + position.getId())) {
                    continue;
                }
                Map<String, Object> map = new HashMap<>(3);
                map.put("viewDistances", position.getViewDistances());
                map.put("height", position.getHeight());
                map.put("position", position.getPositions());
                redisUtil.hmset(RedisName.VIEW_POSITION_DATA_ALL + "_" + position.getId(), map);
            }
        }
        List<SystemsScene> systemsScenes = systemsSceneMapper.selectList(null);
        if (CollectionUtil.isNotEmpty(systemsScenes)) {
            for (SystemsScene systemsScene : systemsScenes) {
                addSystemsScene(systemsScene.getId(), JSON.toJSONString(systemsScene));
            }
        }
    }

    @Override
    public void updateView(SystemPosition position) {
        Map<String, Object> map = new HashMap<>(3);
        map.put("viewDistances", position.getViewDistances());
        map.put("height", position.getHeight());
        map.put("position", position.getPositions());
        redisUtil.del(RedisName.VIEW_POSITION_DATA_ALL + "_" + position.getId());
        redisUtil.hmset(RedisName.VIEW_POSITION_DATA_ALL + "_" + position.getId(), map);
    }

    @Override
    public void deleteTargetRedisData(String targetId) {
        redisUtil.del(
                RedisName.VIEW_POSITION_DATA_ALL + "_" + targetId,
                RedisName.TARGET_BASE_DATA_ALL + "_" + targetId,
                RedisName.TARGET_SPECIAL_DATA_ALL + "_" + targetId,
                RedisName.TARGET_SUB_RELATION_LIST + "_" + targetId
        );
    }

    @Override
    public void deleteSubRedisData(String subId) {
        redisUtil.del(
                RedisName.VIEW_POSITION_DATA_ALL + "_" + subId,
                RedisName.TARGET_BASE_DATA_ALL + "_" + subId,
                RedisName.TARGET_SPECIAL_DATA_ALL + "_" + subId
        );
    }

    @Override
    public void deleteView(String viewId) {
        redisUtil.del(RedisName.VIEW_POSITION_DATA_ALL + "_" + viewId);
    }

    @Override
    public void deleteTypeName(String typeId) {
        redisUtil.hdel(RedisName.SIGN_TARGET_NAME_ALL, typeId);
    }

    @Override
    public void deleteSubTypeName(String typeId) {
        redisUtil.hdel(RedisName.SIGN_SUB_NAME_ALL, typeId);
    }

    @Override
    public void deletePostureTypeName(String typeId) {
        redisUtil.hdel(RedisName.SIGN_POSTURE_NAME_ALL, typeId);
    }

    @Override
    public void deleteSign(String typeId) {
        redisUtil.hdel(RedisName.SIGN_TARGET_ICON_ALL, typeId);
    }

    @Override
    public void deletePosture(String redId, String postureId, boolean isAll) {
        if (isAll) {
            redisUtil.del(RedisName.POSTURE_RELATION_LIST + "_" + redId);
        } else {
            redisUtil.setRemove(RedisName.POSTURE_RELATION_LIST + "_" + redId, postureId);
        }
    }

    @Override
    public Map<String, Object> getTargetMapRedisData(String targetId, String type) {
        Map<String, Object> resultMap = new HashMap<>();
        //基础属性参数初始化
        resultMap.put("area", 0);
        resultMap.put("plotinfoobj", "");
        resultMap.put("targetType", "");
        resultMap.put("icon", "");
        resultMap.put("signList", ListUtil.empty());
        getViewData(resultMap, targetId, true);
        //场景
//        resultMap.put("height", "0");
//        resultMap.put("viewDistances", ListUtil.empty());
//        resultMap.put("position", ListUtil.empty());
//        boolean isPos = true;
//        //场景缓存
//        if (redisUtil.hasKey(RedisName.VIEW_POSITION_DATA_ALL + "_" + targetId)) {
//            Map<Object, Object> viewMap = redisUtil.hmget(RedisName.VIEW_POSITION_DATA_ALL + "_" + targetId);
//            resultMap.put("height", viewMap.get("height"));
//            JSONArray viewDistances = JSONArray.parseArray(viewMap.get("viewDistances").toString());
//            if (CollectionUtil.isNotEmpty(viewDistances)) {
//                resultMap.put("viewDistances", viewDistances);
//            }
//            JSONArray position = JSONArray.parseArray(viewMap.get("position").toString());
//            if (CollectionUtil.isNotEmpty(position)) {
//                resultMap.put("position", position);
//                isPos = false;
//            }
//        }
        //基础参数
        Map<Object, Object> baseMap = redisUtil.hmget(RedisName.TARGET_BASE_DATA_ALL + "_" + targetId);
        resultMap.put("targetName", FileElseUtil.getObjectToString(baseMap.get("targetName")));
//        if (isPos) {
//            resultMap.put("position", FileElseUtil.getObjectToList(baseMap.get("position")));
//        }
        resultMap.put("area", FileElseUtil.getObjectToDouble(baseMap.get("area")));
        resultMap.put("plotinfoobj", FileElseUtil.getObjectToString(baseMap.get("plotinfoobj")));
        //图标
        if (StringUtils.isNotBlank(type)) {
            resultMap.put("targetType", getTypeName(type));
            String sign = getTypeSign(type);
            resultMap.put("icon", this.keyWordSystem.getSERVER_URL() + "/sign/" + sign);
        }
        //作用范围
        getTargetRangeValue(targetId, resultMap);
        return resultMap;
    }

    @Override
    public Map<String, Object> getSubMapRedisData(String subId) {
        Map<String, Object> m = new HashMap<>();
        m.put("subName", "");
        m.put("position", ListUtil.empty());
        m.put("height", 0);
        m.put("area", 0);
        m.put("plotinfoobj", "");
        m.put("targetType", "");
        m.put("icon", "");
        m.put("signList", ListUtil.empty());
        m.put("imgList", ListUtil.empty());
        m.put("modelList", ListUtil.empty());
        m.put("angle", "");
        //基础参数
        Map<Object, Object> baseMap = redisUtil.hmget(RedisName.TARGET_BASE_DATA_ALL + "_" + subId);
        if (ObjectUtil.isEmpty(baseMap)) {
            return null;
        }
        m.put("subName", FileElseUtil.getObjectToString(baseMap.get("targetName")));
        m.put("position", FileElseUtil.getObjectToList(baseMap.get("position")));
        m.put("height", FileElseUtil.getObjectToDouble(baseMap.get("height")));
        m.put("area", FileElseUtil.getObjectToDouble(baseMap.get("area")));
        m.put("plotinfoobj", FileElseUtil.getObjectToString(baseMap.get("plotinfoobj")));
        String classify = TargetUtil.getParamListOrString(baseMap.get("targetClassify").toString());
        if (StringUtils.isNotBlank(classify)) {
            m.put("targetType", getSubTypeName(classify));
            String sign = getTypeSign(classify);
            m.put("icon", this.keyWordSystem.getSERVER_URL() + "/sign/" + sign);
        }
        //作用范围
        getTargetRangeValue(subId, m);
        return m;
    }

    @Override
    public Map<String, Object> getPostureMapRedisData(String subId) {
        Map<String, Object> m = new HashMap<>();
        m.put("subName", "");
        m.put("position", ListUtil.empty());
        m.put("height", 0);
        m.put("area", 0);
        m.put("plotinfoobj", "");
        m.put("targetType", "");
        m.put("icon", "");
        m.put("signList", ListUtil.empty());
        m.put("imgList", ListUtil.empty());
        m.put("modelList", ListUtil.empty());
        m.put("angle", "");
        //基础参数
        Map<Object, Object> baseMap = redisUtil.hmget(RedisName.TARGET_BASE_DATA_ALL + "_" + subId);
        if (ObjectUtil.isEmpty(baseMap)) {
            return null;
        }
        m.put("subName", FileElseUtil.getObjectToString(baseMap.get("targetName")));
        m.put("position", FileElseUtil.getObjectToList(baseMap.get("position")));
        m.put("height", FileElseUtil.getObjectToDouble(baseMap.get("height")));
        m.put("area", FileElseUtil.getObjectToDouble(baseMap.get("area")));
        m.put("plotinfoobj", FileElseUtil.getObjectToString(baseMap.get("plotinfoobj")));
        String classify = TargetUtil.getParamListOrString(baseMap.get("targetClassify").toString());
        if (StringUtils.isNotBlank(classify)) {
            m.put("targetType", getPostureTypeName(classify));
            String sign = getTypeSign(classify);
            m.put("icon", this.keyWordSystem.getSERVER_URL() + "/sign/" + sign);
        }
        return m;
    }

    @Override
    public Map<String, Object> getSubBaseData(String subId) {
        //基础参数
        Map<Object, Object> baseMap = redisUtil.hmget(RedisName.TARGET_BASE_DATA_ALL + "_" + subId);
        if (ObjectUtil.isEmpty(baseMap)) {
            return null;
        }
        Map<String, Object> map = new HashMap<>();
        map.put("subId", subId);
        map.put("targetName", "");
        map.put("targetClassify", "");
        map.put("icon", keyWordSystem.getSERVER_URL() + "/icon/default.jpeg");
        getViewData(map, subId, true);
        map.put("targetName", FileElseUtil.getObjectToString(baseMap.get("targetName")));
        if (ObjectUtil.isNotEmpty(baseMap.get("icon"))) {
            map.put("icon", keyWordSystem.getSERVER_URL() + baseMap.get("icon").toString());
        }
        map.put("targetClassify", TargetUtil.getObjectListOrString(baseMap.get("targetClassify")));
        return map;
    }

    @Override
    public Map<String, Object> getPostureBaseData(String subId) {
        //基础参数
        Map<Object, Object> baseMap = redisUtil.hmget(RedisName.TARGET_BASE_DATA_ALL + "_" + subId);
        if (ObjectUtil.isEmpty(baseMap)) {
            return null;
        }
        Map<String, Object> map = new HashMap<>();
        map.put("postureId", subId);
        map.put("targetName", "");
        map.put("targetClassify", "");
        map.put("icon", keyWordSystem.getSERVER_URL() + "/icon/default.jpeg");
        getViewData(map, subId, true);
        map.put("targetName", FileElseUtil.getObjectToString(baseMap.get("targetName")));
        if (ObjectUtil.isNotEmpty(baseMap.get("icon"))) {
            map.put("icon", keyWordSystem.getSERVER_URL() + baseMap.get("icon").toString());
        }
        map.put("targetClassify", TargetUtil.getObjectListOrString(baseMap.get("targetClassify")));
        return map;
    }

    @Override
    public void getViewData(Map<String, Object> map, String id, boolean isTarget) {
        map.put("position", ListUtil.empty());
        map.put("viewPosition", ListUtil.empty());
        map.put("height", 10000);
        map.put("viewDistances", ListUtil.empty());
        boolean isPos = true;
        //场景缓存
        if (redisUtil.hasKey(RedisName.VIEW_POSITION_DATA_ALL + "_" + id)) {
            Map<Object, Object> viewMap = redisUtil.hmget(RedisName.VIEW_POSITION_DATA_ALL + "_" + id);
            map.put("height", viewMap.get("height"));
            JSONArray viewDistances = JSONArray.parseArray(viewMap.get("viewDistances").toString());
            if (CollectionUtil.isNotEmpty(viewDistances)) {
                map.put("viewDistances", viewDistances);
            }
            JSONArray position = JSONArray.parseArray(viewMap.get("position").toString());
            if (CollectionUtil.isNotEmpty(position)) {
                map.put("viewPosition", position);
            }
        }
        if (isTarget) {
            List position = FileElseUtil.getObjectToList(redisUtil.hget(RedisName.TARGET_BASE_DATA_ALL + "_" + id, "position"));
            if (CollectionUtil.isNotEmpty(position) && position.size() > 2) {
                position.set(2, 0);
            }
            map.put("position", position);
        }
    }

    @Override
    public List<Double> getViewPosition(String id) {
        List<Double> pList = new ArrayList<>();
        List list = FileElseUtil.getObjectToList(redisUtil.hget(RedisName.TARGET_BASE_DATA_ALL + "_" + id, "position"));
        if (CollectionUtil.isNotEmpty(list)) {
            pList.add(FileElseUtil.getStringTransitionDouble(list.get(0).toString()));
            pList.add(FileElseUtil.getStringTransitionDouble(list.get(1).toString()));
            pList.add(0D);
        }
        //场景缓存
//        if (redisUtil.hasKey(RedisName.VIEW_POSITION_DATA_ALL + "_" + id)) {
//            Map<Object, Object> viewMap = redisUtil.hmget(RedisName.VIEW_POSITION_DATA_ALL + "_" + id);
//            JSONArray position = JSONArray.parseArray(viewMap.get("position").toString());
//            if (CollectionUtil.isNotEmpty(position)) {
//                pList = new ArrayList<>();
//                pList.add(position.getDouble(0));
//                pList.add(position.getDouble(1));
//                pList.add(position.getDouble(2));
//            }
//        }
        return pList;
    }

    @Override
    public List<Double> getViewPositionRad(String id) {
        List<Double> pList = new ArrayList<>();
        List list = FileElseUtil.getObjectToList(redisUtil.hget(RedisName.TARGET_BASE_DATA_ALL + "_" + id, "position"));
        if (CollectionUtil.isNotEmpty(list)) {
            pList.add(LngLatUtil.rad(FileElseUtil.getStringTransitionDouble(list.get(0).toString())));
            pList.add(LngLatUtil.rad(FileElseUtil.getStringTransitionDouble(list.get(1).toString())));
            pList.add(0D);
        }
        return pList;
    }

    @Override
    public void getTypeNameAndIcon(String id, int type, Map<String, Object> map) {
        List classify = FileElseUtil.getObjectToList(redisUtil.hget(RedisName.TARGET_BASE_DATA_ALL + "_" + id, "targetClassify"));
        if (CollectionUtil.isEmpty(classify)) {
            map.put("type", "未知");
            map.put("icon", this.keyWordSystem.getSERVER_URL() + "/sign/default.png");
        } else {
            if (1 == type) {
                map.put("type", getTypeName(classify.get(classify.size() - 1).toString()));
            } else if (2 == type) {
                map.put("type", getSubTypeName(classify.get(classify.size() - 1).toString()));
            } else {
                map.put("type", getPostureTypeName(classify.get(classify.size() - 1).toString()));
            }
            map.put("icon", this.keyWordSystem.getSERVER_URL() + "/sign/" + getTypeSign(classify.get(classify.size() - 1).toString()));
        }
    }

    @Override
    public void initSystemsTargetList() {
        List<SystemsMaster> masters = systemsMasterMapper.selectList(null);
        if (CollectionUtil.isNotEmpty(masters)) {
            for (SystemsMaster master : masters) {
                deleteSystemsRelationRangeValue(master.getSysId(), null, true);
                //记录体系下系统集合
                if (!"0".equals(master.getRedId()) && !redisUtil.hHasKey(RedisName.SYSTEMS_RELATION_LIST + "_" + master.getRedId(), master.getSysId())) {
                    updateSystemsRelation(master.getRedId(), master);
                }
                //记录体系各个集合下目标列表
                if (!redisUtil.hasKey(RedisName.SYSTEMS_TARGET_LIST + "_" + master.getSysId())) {
                    List<String> targetIds = systemsRelationMapper.selectSystemsTargetId(master.getSysId());
                    if (CollectionUtil.isNotEmpty(targetIds)) {
                        for (String targetId : targetIds) {
                            redisUtil.sSet(RedisName.SYSTEMS_TARGET_LIST + "_" + master.getSysId(), targetId);
                        }
                    }
                }
            }
        }
    }

    @Override
    public void updateSystemsTarget(String sysId, List<SystemsRelation> list) {
        redisUtil.del(RedisName.SYSTEMS_TARGET_LIST + "_" + sysId);
        for (SystemsRelation systemsRelation : list) {
            redisUtil.sSet(RedisName.SYSTEMS_TARGET_LIST + "_" + sysId, systemsRelation.getTargetId());
        }
    }

    @Override
    public List<String> getSystemsTargetList(String sysId) {
        if (redisUtil.hasKey(RedisName.SYSTEMS_TARGET_LIST + "_" + sysId)) {
            Set<Object> set = redisUtil.sGet(RedisName.SYSTEMS_TARGET_LIST + "_" + sysId);
            List<String> ids = new ArrayList<>();
            for (Object s : set) {
                ids.add(s.toString());
            }
            return ids;
        }
        return ListUtil.empty();
    }

    @Override
    public void deleteSystemsTargetList(String sysId) {
        redisUtil.del(RedisName.SYSTEMS_TARGET_LIST + "_" + sysId);
    }

    @Override
    public void updateSystemsRelation(String sysId, SystemsMaster systemsMaster) {
        String key = RedisName.SYSTEMS_RELATION_LIST + "_" + sysId;
        redisUtil.hset(key, systemsMaster.getSysId(), JSON.toJSONString(systemsMaster));
    }

    @Override
    public Map<Object, Object> getSystemsRelation(String sysId) {
        String key = RedisName.SYSTEMS_RELATION_LIST + "_" + sysId;
        if (redisUtil.hasKey(key)) {
            return redisUtil.hmget(key);
        }
        return null;
    }

    @Override
    public void deleteSystemsRelation(String sysId, String redId) {
        redisUtil.hdel(RedisName.SYSTEMS_RELATION_LIST + "_" + sysId, redId);
    }

    @Override
    public void deleteSystemsRelationAll(String sysId) {
        redisUtil.del(RedisName.SYSTEMS_RELATION_LIST + "_" + sysId);
    }

    @Override
    public void initSystemsRelationRangeValue() {
        List<SystemsRangeType> systemsRangeTypes = systemsRangeTypeMapper.selectList(null);
        if (CollectionUtil.isNotEmpty(systemsRangeTypes)) {
            for (SystemsRangeType systemsRangeType : systemsRangeTypes) {
                if (StringUtils.isBlank(systemsRangeType.getSysId())) {
                    if (redisUtil.hHasKey(RedisName.TARGET_RANGE_LIST, systemsRangeType.getTargetId())) {
                        continue;
                    }
                    redisUtil.hset(
                            RedisName.TARGET_RANGE_LIST,
                            systemsRangeType.getTargetId(),
                            JSON.toJSONString(systemsRangeType)
                    );
                } else {
                    if (null != systemsRangeType.getShowType() && 1 == systemsRangeType.getShowType()) {
                        String redisKey;
                        if (StringUtils.isNotBlank(systemsRangeType.getTargetId())) {
                            redisKey = RedisName.SYSTEMS_TARGET_RANGE_LIST + "_" + systemsRangeType.getSysId() + "_" + systemsRangeType.getTargetId();
                        } else {
                            redisKey = RedisName.SYSTEMS_TARGET_RANGE_LIST + "_" + systemsRangeType.getSysId();
                        }
                        redisUtil.hset(
                                redisKey,
                                systemsRangeType.getId(),
                                JSON.toJSONString(systemsRangeType)
                        );
                    }
                }
            }
        }
    }

    @Override
    public void updateSystemsRelationRangeValue(String sysId, String targetId, String rangeTypeId, String systemsRangeType) {
        String redisKey;
        if (StringUtils.isNotBlank(targetId)) {
            redisKey = RedisName.SYSTEMS_TARGET_RANGE_LIST + "_" + sysId + "_" + targetId;
        } else {
            redisKey = RedisName.SYSTEMS_TARGET_RANGE_LIST + "_" + sysId;
        }
        redisUtil.hset(redisKey, rangeTypeId, systemsRangeType);
    }

    @Override
    public void updateTargetRangeValue(String targetId, String targetRangeType) {
        redisUtil.hset(RedisName.TARGET_RANGE_LIST, targetId, targetRangeType);
    }


    @Override
    public void getSystemsRelationRangeValue(String sysId, String targetId, Map<String, Object> map) {
        String redisKey;
        if (StringUtils.isNotBlank(targetId)) {
            redisKey = RedisName.SYSTEMS_TARGET_RANGE_LIST + "_" + sysId + "_" + targetId;
        } else {
            redisKey = RedisName.SYSTEMS_TARGET_RANGE_LIST + "_" + sysId;
        }
        List<Map<String, Object>> rangeList = new ArrayList<>();
        if (redisUtil.hasKey(redisKey)) {
            Map<Object, Object> hmget = redisUtil.hmget(redisKey);
            for (Object id : hmget.keySet()) {
                Map<String, Object> m = new HashMap<>();
                SystemsRangeType systemsRangeType = JSONObject.parseObject(hmget.get(id).toString(), SystemsRangeType.class);
                m.put("rangeTypeId", systemsRangeType.getId());
                m.put("radius", systemsRangeType.getRadius());
                m.put("color", "");
                m.put("rangeType", "");
                m.put("rangePosition", ListUtil.empty());
                double h = FileElseUtil.getObjectToDouble(systemsRangeType.getHeight());
                m.put("height", h);
                m.put("angle", FileElseUtil.getObjectToDouble(systemsRangeType.getAngle()));
                Object o = redisUtil.hget(RedisName.RANGE_TYPE_LIST, systemsRangeType.getRangeId());
                if (ObjectUtil.isNotEmpty(o)) {
                    RangeType rangeType = JSONObject.parseObject(o.toString(), RangeType.class);
                    if (rangeType.getRangeType().equals("1")) {
                        h = 0;
                    }
                    m.put("rangeType", rangeType.getRangeType());
                    m.put("color", rangeType.getColor());
                }
                List<Double> viewPosition = getViewPosition(targetId);
                m.put("position", viewPosition);
                if (systemsRangeType.getRadius() > 0 && CollectionUtil.isNotEmpty(viewPosition)) {
                    List<Double> list = DeoUtil.computerThatLonLat(viewPosition.get(0), viewPosition.get(1), 0, systemsRangeType.getRadius() * 1000);
                    List<Double> list1 = DeoUtil.computerThatLonLat(viewPosition.get(0), viewPosition.get(1), keyWordSystem.getBrag(), systemsRangeType.getRadius() * 1000 / 2);
                    List<Double> list2 = DeoUtil.computerThatLonLat(viewPosition.get(0), viewPosition.get(1), 270, systemsRangeType.getRadius() * 1000);
                    List<List<Double>> rangePosition = new ArrayList<>();
                    rangePosition.add(Arrays.asList(list.get(0), list.get(1), h * 1000));
                    rangePosition.add(Arrays.asList(list1.get(0), list1.get(1), h * 1000));
                    rangePosition.add(Arrays.asList(list2.get(0), list2.get(1), h * 1000));
                    m.put("rangePosition", rangePosition);
                }
                rangeList.add(m);
            }
        }
        map.put("rangeList", rangeList);
    }

    @Override
    public void getTargetRangeValue(String targetId, Map<String, Object> map) {
        if (redisUtil.hHasKey(RedisName.TARGET_RANGE_LIST, targetId)) {
            SystemsRangeType systemsRangeType = JSONObject.parseObject(redisUtil.hget(RedisName.TARGET_RANGE_LIST, targetId).toString(), SystemsRangeType.class);
            map.put("radius", systemsRangeType.getRadius());
            map.put("color", "");
            map.put("rangeType", "");
            Object o = redisUtil.hget(RedisName.RANGE_TYPE_LIST, systemsRangeType.getRangeId());
            if (ObjectUtil.isNotEmpty(o)) {
                RangeType rangeType = JSONObject.parseObject(o.toString(), RangeType.class);
                map.put("rangeType", rangeType.getRangeType());
                map.put("color", rangeType.getColor());
            }
        } else {
            map.put("radius", 0);
            map.put("rangeType", "");
            map.put("color", "");
        }
    }

    @Override
    public void deleteSystemsRelationRangeValue(String sysId, String targetId, boolean isAll) {
        if (isAll) {
            redisUtil.del(RedisName.SYSTEMS_TARGET_RANGE_LIST + "_" + sysId);
        } else {
            redisUtil.del(RedisName.SYSTEMS_TARGET_RANGE_LIST + "_" + sysId + "_" + targetId);
        }
    }

    @Override
    public void deleteTargetRangeValue(String targetId, boolean isAll) {
        if (isAll) {
            redisUtil.del(RedisName.TARGET_RANGE_LIST);
        }
        redisUtil.hdel(RedisName.TARGET_RANGE_LIST, targetId);
    }

    @Override
    public void initRangeType() {
        redisUtil.del(RedisName.RANGE_TYPE_LIST);
        List<RangeType> types = rangeTypeMapper.selectList(null);
        if (CollectionUtil.isNotEmpty(types)) {
            for (RangeType type : types) {
                redisUtil.hset(RedisName.RANGE_TYPE_LIST, type.getId(), JSON.toJSONString(type));
            }
        }
    }

    @Override
    public void updateRangeType(String rangeId, String rangeType) {
        redisUtil.hset(RedisName.RANGE_TYPE_LIST, rangeId, rangeType);
    }

    @Override
    public void deleteRangeType(String rangeId) {
        redisUtil.hdel(RedisName.RANGE_TYPE_LIST, rangeId);
    }

    @Override
    public void updateTargetMeans(String targetId, String means) {
        redisUtil.hset(RedisName.TARGET_MEANS_LIST, targetId, means);
    }

    @Override
    public String getTargetMeans(String targetId) {
        if (redisUtil.hHasKey(RedisName.TARGET_MEANS_LIST, targetId)) {
            return FileElseUtil.getObjectToString(redisUtil.hget(RedisName.TARGET_MEANS_LIST, targetId));
        }
        return "无";
    }

    @Override
    public void addSystemsScene(String id, String scene) {
        redisUtil.hset(RedisName.SYSTEMS_SCENE, id, scene);
    }

    @Override
    public void getSystemsScene(String id, Map<String, Object> map) {
        map.put("viewPosition", ListUtil.empty());
        map.put("viewRotation", "");
        if (redisUtil.hHasKey(RedisName.SYSTEMS_SCENE, id)) {
            SystemsScene scene = JSONObject.parseObject(redisUtil.hget(RedisName.SYSTEMS_SCENE, id).toString(), SystemsScene.class);
            map.put("viewPosition", FileElseUtil.getObjectToList(scene.getViewPosition()));
            map.put("viewRotation", FileElseUtil.getObjectToList(scene.getViewRotation()));
        }
    }

    @Override
    public Set<String> selectSystemsTargetIds(String sysId) {
        Set<String> targetIds = new HashSet<>();
        //查询体系下所有目标ID
        if (redisUtil.hasKey(RedisName.SYSTEMS_TARGET_LIST + "_" + sysId)) {
            Set<Object> targetSet = redisUtil.sGet(RedisName.SYSTEMS_TARGET_LIST + "_" + sysId);
            for (Object id : targetSet) {
                if (redisUtil.hasKey(RedisName.TARGET_BASE_DATA_ALL + "_" + id)) {
                    targetIds.add(id.toString());
                }
            }
        }
        if (redisUtil.hasKey(RedisName.SYSTEMS_RELATION_LIST + "_" + sysId)) {
            Map<Object, Object> relation = getSystemsRelation(sysId);
            for (Object o : relation.keySet()) {
                SystemsMaster system = JSONObject.parseObject(relation.get(o).toString(), SystemsMaster.class);
                if (redisUtil.hasKey(RedisName.SYSTEMS_TARGET_LIST + "_" + system.getSysId())) {
                    Set<Object> targetSet = redisUtil.sGet(RedisName.SYSTEMS_TARGET_LIST + "_" + system.getSysId());
                    for (Object id : targetSet) {
                        if (redisUtil.hasKey(RedisName.TARGET_BASE_DATA_ALL + "_" + id)) {
                            targetIds.add(id.toString());
                        }
                    }
                }
            }
        }
        return targetIds;
    }

    @Override
    public void addIndexLevelValue(String id, String level, String name, double value) {
        String redisName = RedisName.SYSTEMS_TARGET_SUB_LEVEL_VALUE + "_" + id + "_" + level;
        redisUtil.hset(redisName, name, value);
    }

    @Override
    public double getIndexLevelValue(String id, String level, String name) {
        String redisName = RedisName.SYSTEMS_TARGET_SUB_LEVEL_VALUE + "_" + id + "_" + level;
        if (redisUtil.hasKey(redisName)) {
            return FileElseUtil.getObjectToDouble(redisUtil.hget(redisName, name));
        }
        return 0;
    }

    @Override
    public void deleteIndexLevelValue(String id, String level, String name) {
        String redisName = RedisName.SYSTEMS_TARGET_SUB_LEVEL_VALUE + "_" + id + "_" + level;
        if (StringUtils.isNotBlank(name)) {
            redisUtil.hdel(redisName, name);
        } else {
            redisUtil.del(redisName);
        }
    }

    @Override
    public void updateFont(String name, String data) {
        redisUtil.hset(RedisName.SYSTEM_FONT_DATA, name, data);
    }

    @Override
    public FontTable getFont(String name) {
        if (redisUtil.hHasKey(RedisName.SYSTEM_FONT_DATA, name)) {
            return JSONObject.parseObject(redisUtil.hget(RedisName.SYSTEM_FONT_DATA, name).toString(), FontTable.class);
        } else {
            FontTable fontTable = new FontTable();
            fontTable.setFontName(name);
            if ("体系名称".equals(name)) {
                fontTable.setFontSize(52);
            } else {
                fontTable.setFontSize(28);
            }
            fontTable.setFontColor("rgba(20, 249, 242, 1)");
            return fontTable;
        }
    }

    @Override
    public String getUserId(String id)  {
        return redisUtil.get("USER_KUNMING_USERID_" + id).toString();
    }

}
